package com.ssh.controllor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.entity.User;
import com.ssh.service.UserService;

import net.sf.json.JSONObject;

@Controller  
//@RequestMapping("/user")  
public class UserController extends ActionSupport{  
	
	final Logger logger = LoggerFactory.getLogger(UserController.class);
	
    @Autowired  
    private UserService userService;  	
    
    //访问路径 http://localhost:8082/SSH/logger
//    @RequestMapping("/logger")
    public void handleRequest ()throws Exception {
    	 HttpServletResponse resp = ServletActionContext.getResponse();
         resp.getWriter().println("test logger");
         
         logger.info("这是一条日志信息 - {}", "jiafuwei");
    }
    //访问路径 http://localhost:8082/SSH/save?username=aaa&password=bbb
    public void save() throws Exception{ 
    	HttpServletRequest request = ServletActionContext.getRequest();
    	HttpServletResponse resp = ServletActionContext.getResponse();
        String username = request.getParameter("username");  
        String password = request.getParameter("password");  
        System.out.println(username + " " + password);  
        System.out.println("controller save...");
        User user1 = new User();
        user1.setUsername(username);
        user1.setPassword(password);
        request.setAttribute("user", user1);
        
        User user2 = userService.selectById(1);
        System.out.println("密码："+user2.getPassword());
        try {
			resp.getWriter().println(user2.getRole());;
		} catch (IOException e) {
			e.printStackTrace();
		}  
    }  
    //访问路径 http://localhost:8082/SSH/tiaozhuan
    public void tiaozhuan() throws Exception{ 
    	HttpServletRequest request = ServletActionContext.getRequest();
    	HttpServletResponse response = ServletActionContext.getResponse();
    	//这个只能跳转项目内部url
    	request.getRequestDispatcher("http://www.baidu.com").forward(request, response);
    	return;
    }
  //访问路径 http://localhost:8082/SSH/tiaozhuan1
    public void tiaozhuan1() throws Exception{ 
    	HttpServletRequest request = ServletActionContext.getRequest();
    	HttpServletResponse response = ServletActionContext.getResponse();
    	
    	response.sendRedirect("http://www.baidu.com");
    	return;
    }
    
    //访问路径 http://localhost:8082/SSH/view/jiafuwei
    /**Spring MVC RESTful JSON**/
    @RequestMapping(value = "/view/{username}", method = RequestMethod.GET)
    @ResponseBody
    public User view(@PathVariable String username){
        User user = new User();
        user.setPassword("123");
        user.setUsername(username);
        System.out.println("view username:"+username);

        return user;
    }

   

  
    //访问路径  http://localhost:8082/SSMProject/user/list
    public void listPerson() throws Exception{

        User user = new User();
        user.setPassword("123");
        user.setUsername("haha");
        
        User user2 = new User();
        user2.setPassword("frekkk");
        user2.setUsername("zheshi");
        
        List<User> listUser = new ArrayList<User>();
        listUser.add(user);
        listUser.add(user2);

        JSONObject jsonObject = new JSONObject();
		jsonObject.put("listUser",listUser);
		HttpServletResponse resp = ServletActionContext.getResponse();
		resp.getWriter().println(jsonObject);
    }
    

}
